// package/pages/rechargeSuccess/rechargeSuccess.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
const app = getApp();
var interval = null //倒计时函数
var againinterval = null //倒计时函数
var orderinterval = null //倒计时函数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTime: 5,
    time: '5秒后返回',
    orderInfo: {},
    type: null,
    resultMsg: '充值中...'
  },

  getCode: function (options) {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒后返回'
      })
      if (currentTime <= 0) {
        clearInterval(interval);
        clearInterval(againinterval);
        clearInterval(orderinterval);
        if (that.data.type == 1) {
          my.navigateBack({
            delta: 1
          })
        } else if (that.data.type == 2) {
          my.redirectTo({
            url: `/member/pages/coupon/coupon`
          })
        } else if (that.data.type == 3) {
          my.redirectTo({
            url: `/member/pages/giftCoupon/giftCoupon`
          })
        }
      }
    }, 1000)
  },
  toback() {
    const that = this;
    clearInterval(interval);
    clearInterval(againinterval);
    clearInterval(orderinterval);
    if (that.data.type == 1) {
      my.navigateBack({
        delta: 1
      })
    } else if (that.data.type == 2) {
      my.redirectTo({
        url: `/member/pages/coupon/coupon`
      })
    } else if (that.data.type == 3) {
      my.redirectTo({
        url: `/member/pages/giftCoupon/giftCoupon`
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.setData({
      type: options.type
    })
    that.getProvince(options.orderId);
    that.getCheckOrder(options.orderId)

    // that.setData({
    //   type: 3
    // })
    // that.getProvince(106838);
    // that.getCheckOrder(106838)
  },
  getProvince(e) {
    const that = this;
    let data = {
      order_id: e
    }
    my.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.getmembermoneyorder, data).then(res => {
      my.hideLoading();
      if (res.data.status == 1) {
        that.getCode();
        againinterval = setInterval(function () {
          that.aginGetdata(e);
        }, 2000) //单位ms，会不断调用

        that.setData({
          orderInfo: res.data.result.order_info
        })
      } else {
        my.showToast({
          content: res.data.msg,
          type: 'exception',

        })
      }
      that.setData({
        hiddenLoading: true
      })
    })
  },

  aginGetdata(e) {
    const that = this;
    let data = {
      order_id: e
    }
    myRequest.EasyHttp.post(api.reqUrls.getmembermoneyorder, data).then(res => {
      if (res.data.status == 1) {
        that.setData({
          orderInfo: res.data.result.order_info
        })
      } else {
        my.showToast({
          content: res.data.msg,
          type: 'exception',
        })
      }
      that.setData({
        hiddenLoading: true
      })
    })
  },
  getCheckOrder(e) {
    const that = this;
    let data = {
      order_id: e
    }
    myRequest.EasyHttp.post(api.reqUrls.checkOrder, data).then(res => {
      if (res.data.status == 1) {
        let resultMsg = that.data.resultMsg;
        if (res.data.result.status == 1) {
          resultMsg = '充值中...';
        } else if (res.data.result.status == 2) {
          resultMsg = '充值成功';
        } else if (res.data.result.status == 3) {
          resultMsg = '充值失败';
        }
        that.setData({
          resultMsg
        })
        orderinterval = setInterval(function () {
          that.getCheckOrders(e);
        }, 1500) //单位ms，会不断调用
      }
    })
  },
  getCheckOrders(e) {
    const that = this;
    let data = {
      order_id: e
    }
    myRequest.EasyHttp.post(api.reqUrls.checkOrder, data).then(res => {
      if (res.data.status == 1) {
        let resultMsg = that.data.resultMsg;
        if (res.data.result.status == 1) {
          resultMsg = '充值中...';
        } else if (res.data.result.status == 2) {
          resultMsg = '充值成功';
          that.setData({
            resultMsg
          })
          setTimeout(() => {
            that.toback()
          }, 1000)
        } else if (res.data.result.status == 3) {
          resultMsg = '充值失败';
        }
        that.setData({
          resultMsg
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(interval);
    clearInterval(againinterval);
    clearInterval(orderinterval);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})